package com.example.mynotification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    NotificationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("1", "渠道名", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
    }

    public void sendNomal(View view) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1");
        Notification notification = builder.setContentText("我是通知文本")
                .setContentTitle("我是通知标题")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .build();
        manager.notify(2, notification);
    }

    public void sendCalcel(View view) {
        manager.cancelAll();
    }

    public void sendWithText(View view) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1");
        Notification notification = builder.setContentTitle("标题")
                .setContentText("123398")
                .setStyle(new NotificationCompat.BigTextStyle().bigText("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"))
                .setSmallIcon(R.drawable.ic_launcher_background)
                .build();
        manager.notify(4, notification);
    }

    public void sendIcon(View view) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1");
        Notification notification = builder
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.img))
                .build();
        manager.notify(5, notification);
    }

    public void sendAction(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1");
        Notification notification = builder.setContentTitle("回到主页面")
                .setContentText("111")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(3, notification);
    }
}